package com.sdkx.wear_os.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter4.BaseQuickAdapter
import com.sdkx.wear_os.bean.PopupBean
import com.sdkx.wear_os.databinding.PopupItemBinding
import com.sdkx.wear_os.utils.GlideEngine

/**
 * PopupWindow适配器，结合PopupWindowView使用
 */
class PopupWindowAdapter : BaseQuickAdapter<PopupBean, PopupWindowAdapter.VH>() {

    class VH(var binding: PopupItemBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onBindViewHolder(holder: VH, position: Int, item: PopupBean?) {
        holder.binding.run {
            if (position == itemCount - 1) {
                //如果是最后一项隐藏线
                popupItemLine.visibility = View.INVISIBLE
            } else {
                popupItemLine.visibility = View.VISIBLE
            }
            item?.let { item ->
                //线背景透明
                popupItemLine.background.mutate().alpha = 100
                popupItemTv.text = item.title
                when (item.imageId) {
                    //无图片
                    null -> {
                        popupItemIv.visibility = View.GONE
                    }
                    //图片int类型
                    is Int -> {
                        popupItemIv.setImageResource(item.imageId as Int)
                    }
                    else -> {
                        GlideEngine.createGlideEngine()
                            .loadImage(context, item.imageId as String, popupItemIv)
                    }
                }
            }

        }
    }

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH {
        return VH(PopupItemBinding.inflate(LayoutInflater.from(context), parent, false))
    }
}